package com.jingdianjichi.auth.domain.service.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jingdianjichi.auth.common.enums.AuthPermissionIsShowEnum;
import com.jingdianjichi.auth.common.enums.AuthPermissionStausEnum;
import com.jingdianjichi.auth.common.enums.IsDeletedEnum;
import com.jingdianjichi.auth.domain.convert.AuthPermissionConverter;
import com.jingdianjichi.auth.domain.entity.AuthPermissionBO;
import com.jingdianjichi.auth.domain.redis.RedisUtil;
import com.jingdianjichi.auth.domain.service.AuthPermissionDomainService;
import com.jingdianjichi.auth.infra.basic.entity.AuthPermission;
import com.jingdianjichi.auth.infra.basic.entity.AuthUser;
import com.jingdianjichi.auth.infra.basic.entity.AuthUserRole;
import com.jingdianjichi.auth.infra.basic.service.AuthPermissionService;
import com.jingdianjichi.auth.infra.basic.service.AuthRolePermissionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AuthPermissionDomainServiceImpl implements AuthPermissionDomainService {

    @Resource
    AuthPermissionService authPermissionService;

    @Resource
    AuthRolePermissionService authRolePermissionService;

    private String authPermissonPrefix = "auth.permisson";
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Boolean add(AuthPermissionBO authPermissionBO) {
        AuthPermission authPermission = AuthPermissionConverter.INSTANCE.converterBoToAuthPermission(authPermissionBO);
        authPermission.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
        authPermission.setStatus(AuthPermissionStausEnum.OPEN.getCode());
        authPermission.setShow(AuthPermissionIsShowEnum.OPEN.getCode());
        Integer insert = authPermissionService.insert(authPermission);
        return insert > 0;
    }

    @Override
    public Boolean update(AuthPermissionBO authPermissionBO) {
        AuthPermission authPermission = AuthPermissionConverter.INSTANCE.converterBoToAuthPermission(authPermissionBO);
        Integer update = authPermissionService.update(authPermission);
        return update > 0;
    }

    @Override
    public Boolean delete(AuthPermissionBO authPermissionBO) {
        AuthPermission authPermission = AuthPermissionConverter.INSTANCE.converterBoToAuthPermission(authPermissionBO);
        authPermission.setIsDeleted(IsDeletedEnum.DELETED.getCode());
        Integer update = authPermissionService.update(authPermission);
        return update > 0;
    }

    @Override
    public List<String> getPermission(String userName) {
        String permissionKey = redisUtil.buildKey(authPermissonPrefix, userName);
        String permissionValue  = redisUtil.get(permissionKey);
        if (StringUtils.isBlank(permissionValue)) {
            return Collections.emptyList();
        }
        List<AuthPermission> permissionList = new Gson().fromJson(permissionValue,
                new TypeToken<List<AuthPermission>>() {
                }.getType());
        List<String> authList = permissionList.stream().map(AuthPermission::getPermissionKey).collect(Collectors.toList());
        return authList;
    }
}
